package me.yuzhou.weixin.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NameCoderWrapper;

import me.yuzhou.weixin.po.Image;
import me.yuzhou.weixin.po.ImageMessage;
import me.yuzhou.weixin.po.Music;
import me.yuzhou.weixin.po.MusicMessage;
import me.yuzhou.weixin.po.News;
import me.yuzhou.weixin.po.NewsMessage;
import me.yuzhou.weixin.po.WeiXinMessage;

/**
 * @author yuzhou
 *
 */
public class MessageUtil {
	
	public static final String MESSAGE_NEWS = "news";
	public static final String MESSAGE_TEXT = "text";
	public static final String MESSAGE_IMAGE = "image";
	public static final String MESSAGE_VOICE = "voice";
	public static final String MESSAGE_VIDEO = "video";
	public static final String MESSAGE_MUSIC = "music";
	
	public static final String MESSAGE_SHORTVIDEO = "shortvideo";
	public static final String MESSAGE_LOCATION = "location";
	public static final String MESSAGE_LINK = "link";
	
	public static final String MESSAGE_EVENT = "event";
	public static final String EVENT_SUBSCRIBE = "subscribe";
	public static final String EVENT_UNSUBSCRIBE = "unsubscribe";
	public static final String EVENT_CLICK = "CLICK";
	public static final String EVENT_VIEW = "VIEW";
	public static final String EVENT_SCANCODE = "scancode_push";
	
	
	
	/** 
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static Map<String, String> xmlToMap(HttpServletRequest request) 
			throws IOException, DocumentException {
		Map<String, String> map = new HashMap<String, String>();
		
		SAXReader reader = new SAXReader();
		
		InputStream ins = request.getInputStream();
		Document doc = reader.read(ins);
		
		Element root = doc.getRootElement();
		
		@SuppressWarnings("unchecked")
		List<Element> list = root.elements();
		
		for(Element element : list) {
			
			map.put(element.getName(), element.getText());
		}
		
		ins.close();
			
		return map;
	}
	
	public static String textMessageToXml(WeiXinMessage message){
		
		XStream xStream = new XStream();
		xStream.alias("xml", message.getClass());
		return xStream.toXML(message);
	}
	
	public static String imageMessageToXml(ImageMessage message){
		
		XStream xStream = new XStream();
		xStream.alias("xml", message.getClass());
		return xStream.toXML(message);
	}
	
	public static String musicMessageToXml(MusicMessage message){
		
		XStream xStream = new XStream();
		xStream.alias("xml", message.getClass());
		return xStream.toXML(message);
	}
	
	/**
	 * @param message
	 * @return
	 */
	public static String newsMessageToXml(NewsMessage message){
		
		XStream xStream = new XStream();
		xStream.alias("xml", message.getClass());
		xStream.alias("item", News.class);
		return xStream.toXML(message);
	}
	
	public static String initText(String toUserName, String fromUserName, String content) {
		WeiXinMessage text= new WeiXinMessage();
		text.setFromUserName(toUserName);
		text.setToUserName(fromUserName);
		text.setMsgType(MessageUtil.MESSAGE_TEXT);
		text.setCreateTime((new Date().getTime())/1000);
		text.setContent(content);
		
		return MessageUtil.textMessageToXml(text);
	}
	
	/**
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 */
	public static String initNewsMessage(String toUserName, String fromUserName) {
		
		String message = null;
		
		List<News> newsList = new ArrayList<News>();
		
		NewsMessage newsMessage = new NewsMessage();
		
		newsMessage.setFromUserName(toUserName);
		newsMessage.setToUserName(fromUserName);
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setMsgType(MessageUtil.MESSAGE_NEWS);
		
		News news = new News();
		news.setTitle("幕课网介绍");
		news.setDescription("慕课网（IMOOC）是国内最大的IT技能学习平台。慕课网提供了丰富的移动端开发、php开发、web前端、android开发以及html5等视频教程资源公开课。并且富有交互性及趣味性，你还可以和朋友一起编程。");
		news.setPicUrl("http://static.mukewang.com/static/img/common/logo.png");
		news.setUrl("www.imooc.com");
		
		newsList.add(news);
		
		newsMessage.setArticles(newsList);
		newsMessage.setArticleCount(newsList.size());
		
		message = newsMessageToXml(newsMessage);
		
		return message;
	}
	
	//Media ID: wAiUcBtXriKe1xRF-HKYIGblu-4yARwLSqT5JS209_JrPj-adjUiOPtEMrsYLi5l
	//{"type":"image","media_id":"MZPt6oc4AWGfRkJHvsZAjd8IN95qNcgfdogaE0ZxJ8zRrkgLDL9XDpYqDqZFoJMX","created_at":1457406414}
	//Media ID: MZPt6oc4AWGfRkJHvsZAjd8IN95qNcgfdogaE0ZxJ8zRrkgLDL9XDpYqDqZFoJMX
	
	public static String initImageMessage(String toUserName, String fromUserName) {
		String message = null;
		
		Image image = new Image();
		image.setMediaId("MZPt6oc4AWGfRkJHvsZAjd8IN95qNcgfdogaE0ZxJ8zRrkgLDL9XDpYqDqZFoJMX");
		
		ImageMessage imageMessage = new ImageMessage();
		imageMessage.setFromUserName(toUserName);
		imageMessage.setToUserName(fromUserName);
		imageMessage.setMsgType(MESSAGE_IMAGE);
		imageMessage.setCreateTime(new Date().getTime());
		imageMessage.setImage(image);
		
		message = imageMessageToXml(imageMessage);
		
		return message;
	}
	
	// {"type":"thumb","thumb_media_id":"5GopA1Kvml7rvWAgF6btsyyL8dQixaSJrV-Ry0jlammIVsN-TuGKfOuCeH8T4NLt","created_at":1457408858}
	// Media ID: 5GopA1Kvml7rvWAgF6btsyyL8dQixaSJrV-Ry0jlammIVsN-TuGKfOuCeH8T4NLt

	public static String initMusicMessage(String toUserName, String fromUserName) {
		String message = null;
		
		Music music = new Music();
		music.setThumbMediaId("5GopA1Kvml7rvWAgF6btsyyL8dQixaSJrV-Ry0jlammIVsN-TuGKfOuCeH8T4NLt");
		music.setTitle("see you again");
		music.setDescription("速7片尾曲");
		String musicUrl = "http://557cd8b8.ngrok.io/mvn-web/resource/baby.mp3";
		music.setMusicUrl(musicUrl);
		music.setHQMusicUrl(musicUrl);
		
		MusicMessage musicMessage = new MusicMessage();
		musicMessage.setFromUserName(toUserName);
		musicMessage.setToUserName(fromUserName);
		musicMessage.setMsgType(MESSAGE_MUSIC);
		musicMessage.setCreateTime(new Date().getTime());
		musicMessage.setMusic(music);
		
		message = musicMessageToXml(musicMessage);
		
		return message;
	}
			
	/** 主菜单 
	 * @return
	 */
	public static String menuText(){
		StringBuffer sb = new StringBuffer();
		sb.append("欢迎您的关注，你按照菜单提示进行操作: \n\n");
		sb.append("1. 课程介绍\n");
		sb.append("2. 幕课网介绍\n");
		sb.append("输入?回到此主菜单\n");
		return sb.toString();
	}
	
	public static String firstMenuText(){
		StringBuffer sb = new StringBuffer();
		sb.append("本课程介绍的是微信公众号开发");
		return sb.toString();
	}
	
	public static String secondMenuText(){
		StringBuffer sb = new StringBuffer();
		sb.append("欢迎您来到幕课网进行学习");
		return sb.toString();
	}

}
